import Taro from '@tarojs/taro-rn';
import * as tslib_1 from "tslib";
import React from 'react';
import { Component } from "@tarojs/taro-rn";
import { View, Image } from "@tarojs/components-rn";
import { toJS } from 'mobx';
import { inject, observer } from "@tarojs/mobx-rn";
import { getRuntime, partOf, pageSize } from "../../utils/common";
import activityStyleSheet from "./activity_styles";
var _styleSheet = activityStyleSheet;
let Index = class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      banners: [],
      pageIndex: 1,
      pageSize,
      totalCount: 0,
      items: []
    };
  }
  componentWillMount() {
    Taro.setNavigationBarTitle({ title: '公益活动' });
    this.getBanners();
    this.getPublicWelfare();
  }
  handelRouter(url = '/pages/index/index') {
    getRuntime().navigateTo(url);
  }
  getBanners() {
    let { PublicWelfareStore } = this.props.stores;
    PublicWelfareStore.getBanners({
      callback: banners => {
        let resArr = toJS(banners);
        console.log(resArr);
        this.setState({
          banners: resArr.items
        });
      }
    });
  }
  getPublicWelfare() {
    let { pageIndex, pageSize } = this.state;
    let { PublicWelfareStore } = this.props.stores;
    PublicWelfareStore.getPagedActivitiesInfo({
      pageIndex,
      pageSize,
      callback: res => {
        if (pageIndex === 1) {
          this.setState({ totalCount: toJS(res.totalCount), items: toJS(res.items) });
        } else {
          const { items } = this.state;
          items.push(...(res.items || []));
          this.setState({ totalCount: res.totalCount, items });
        }
      }
    });
  }
  onReachBottom() {
    let { totalCount, pageSize, pageIndex } = this.state;
    const { loading } = this.props.stores.homeStore;
    if (totalCount > pageIndex * pageSize && !loading) {
      this.setState({
        pageIndex: pageIndex + 1
      }, () => {
        this.getPublicWelfare();
      });
    }
  }
  render() {
    let { items } = this.state;
    return <View style={_styleSheet["welfare-index"]}>
        {items.length > 0 && <View style={_styleSheet["list-17"]}>
          {items.map(item => {
          return <View onClick={this.handelRouter.bind(this, '/pages/public_welfare/activity_detail?id=' + item.id)} key={item.id} style={_styleSheet["list-17-item"]}>
                  <Image src={item.logoImg} style={_styleSheet["img"]} />
                  <View style={_styleSheet["info"]}>
                    <View style={_styleSheet["list-title"]}>{item.title}</View>
                    <View style={_styleSheet["list-desc"]}>{partOf(item.introduce, 50)}</View>
                  </View>
                </View>;
        })}
        </View>}
        {items.length <= 0 && <View style={_styleSheet["page-none-info"]}>没有更多信息了。。。</View>}
      </View>;
  }
};
Index = tslib_1.__decorate([inject("stores"), observer], Index);
export default Index;